package Domain.Player;

import Domain.GameGrid;
import Domain.Square;
import java.awt.Color;
import java.io.Serializable;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Human represents a Human player
 */
public class Human extends Player implements Serializable{

	private Square nextMove = new Square();
	private boolean nextMoveSet = false;
	private Lock lock = new ReentrantLock();
	private Condition newMove = lock.newCondition();

	public Human(String name, Disc discID, Color discColor) {
		super(name, discID, discColor);
	}

	public void setNextMove(int row, int column) {
		lock.lock();
		try {
			nextMoveSet = true;
                        this.nextMove.setX(row);
			this.nextMove.setY(column);
			newMove.signalAll();
		} finally {
			lock.unlock();
		}
	}

	@Override
	public Square getNextMove(GameGrid gamegrid) {
		lock.lock();
		try {
			while (nextMoveSet == false) {
				newMove.await();
			}
		} catch (InterruptedException ex) {
			System.out.println("InterruptedException: " + ex);
		} finally {
			lock.unlock();
		}
		nextMoveSet = false;
		return nextMove;
	}

	@Override
	public PlayerType getType() {
		return PlayerType.HUMAN;
	}

	@Override
	public String toString() {
		return super.toString()
				+ ", type = human, color = " + getDiscColor();
	}

    @Override
    public void updateHash(double reward) {
        
    }
}
